package com.mediacenter.application;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.android.apptools.OpenHelperManager.SqliteOpenHelperFactory;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.mediacenter.PreparedResource;
import com.mediacenter.site.model.DatabaseHelper;
import com.mediacenter.site.model.Show;
import com.mediacenter.site.model.Source;
import com.mediacenter.site.pptv.PPTVSource;
import com.mediacenter.site.souhu.SohuSource;
import com.mediacenter.site.tencent.TencentSource;
import com.mediacenter.site.youku.YoukuSource;

public class MApplication extends OrmLiteBaseApplication<DatabaseHelper>{
	public static String CACHE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mediacenter/imgcache/";
	private static MApplication instance ;
	public static MApplication getInstance(){
		return instance;
	}
	private PreparedResource mResource;
	private List<Source> mSources;
	private Source currentSource;
	private Show currentShow;
	
	public PreparedResource getPreparedResource(){
		return mResource;
	}
	static {
		OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {
			public OrmLiteSqliteOpenHelper getHelper(Context context) {
				return new DatabaseHelper(context);
			}
		});
	}

	@Override
    public void onCreate() {
		instance = this;
		mResource = new PreparedResource(this);
		mSources = new ArrayList<Source>();
		mSources.add(new YoukuSource());
        mSources.add(new TencentSource());
        mSources.add(new SohuSource());
        mSources.add(new PPTVSource());
        currentSource = mSources.get(1);
		
	}
	
	public Show getCurrentShow() {
		return currentShow;
	}
	
	public void setCurrentShow(Show currentShow) {
		this.currentShow = currentShow;
	}
	
	public Source getCurrentSource() {
		return currentSource;
	}

	public void setCurrentSource(Source currentSource) {
		this.currentSource = currentSource;
	}
	
	public void setCurrentSource(String name) {
		Source nameSource = null;
		for (Source source : mSources) {
			if (name.equals(source.getSourceThumb())) {
				nameSource = source;
				break;
			}
		}
		this.currentSource = nameSource;
	}
	
	public Source findCurrentSource(String name) {
		if (name == null) {
			return null;
		}
		for (Source source : mSources) {
			if (name.equals(source.getSourceThumb())) {
				return source;
			}
		}
		return null;
	}

	public List<Source> getSources() {
		return mSources;
	}

	public List<Show> getSourceShows() {
		List<Show> sources = new ArrayList<Show>();
		for (Source source : mSources) {
			Show show = new Show();
			show.setTitle(source.getSourceThumb());
			show.setFace(source.getFace());
			sources.add(show);
		}
		return sources;
	}
}
